using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Srvtools;

namespace cGqtQtn
{
    public partial class Form1 : InfoForm
    {
        public Form1()
        {
            InitializeComponent();
        }

        ////////////
        public string GetToday()
        {
            return string.Format("{0:yyyyMMdd}", DateTime.Today);
        }

        public string GetNowTime()
        {

            return string.Format("{0:hhmmss}", DateTime.Now);
        }

        public string GetUser()
        {
            return CliUtils.fLoginUser;
        }
        /////////////

        private void infoDataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void ibsMaster_BeforeEdit(object sender, EventArgs e)
        {
            //在新增就顯示建檔時間 
            infoTextBox6.Text = DateTime.Now.ToLongTimeString();                   
        }
           

        private void infoDataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if ((infoNavigator1.GetCurrentState() == "Inserting") || (infoNavigator1.GetCurrentState() == "Editing"))
            {                
                //計算總價欄位等於售價﹡數量                              
                float amt = 0;
                string Qty = infoDataGridView1.Columns[e.ColumnIndex].DataPropertyName.ToString();
                if (Qty == "QT_QTY")
                {
                    amt = float.Parse(((DataRowView)ibsDetail.Current)["PR_SALES"].ToString());
                    amt = amt * float.Parse(((DataRowView)ibsDetail.Current)["QT_QTY"].ToString());
                    ((DataRowView)ibsDetail.Current)["AM_SALES"] = amt.ToString();
                    ibsDetail.EndEdit();

                    //重新計算金額
                    double RI_TAX = 0, AM_EXCHG = 0, AM_CURRENCY = 0, AM_TOTAL = 0, RI_EXCHG = 0, AM_TAX = 0;

                    //RI_TAX為稅率
                    if (InfoComboBox0.Text == "免稅")
                        RI_TAX = 0;
                    else if (InfoComboBox0.Text == "應稅")
                        RI_TAX = 0.05;

                    if (InfoTextBox07.Text == "")
                        AM_CURRENCY = 0;
                    else
                    {
                        AM_CURRENCY = Convert.ToDouble(infoDataGridView1.DoSum(9, totalMode.sum));//報價金額
                        RI_EXCHG = Convert.ToDouble(InfoTextBox16.Text);//匯率
                        AM_EXCHG = AM_CURRENCY * RI_EXCHG;//換算金額
                        AM_TAX = AM_EXCHG * RI_TAX;//稅額
                        AM_TOTAL = AM_EXCHG + AM_TAX;//總金額(含稅)                      
                        //把值丟入TEXTBOX
                        InfoTextBox07.Text = AM_CURRENCY.ToString();//報價金額
                        InfoTextBox1.Text = AM_EXCHG.ToString();//換算金額
                        InfoTextBox31.Text = AM_TAX.ToString();//稅額
                        InfoTextBox21.Text = AM_TOTAL.ToString();//總金額(含稅)                  
                    }
                }


            }

        }


        private void InfoComboBox0_TextChanged(object sender, EventArgs e)
        {
            if ((infoNavigator1.GetCurrentState() == "Inserting") || (infoNavigator1.GetCurrentState() == "Editing"))
            {
                //計算金額,稅別改變時
                double RI_TAX = 0, AM_EXCHG = 0, AM_CURRENCY = 0, AM_TOTAL = 0, RI_EXCHG = 0, AM_TAX = 0;

                //RI_TAX為稅率
                if (InfoComboBox0.Text == "免稅")
                    RI_TAX = 0;
                else if (InfoComboBox0.Text == "應稅")
                    RI_TAX = 0.05;

                if (InfoTextBox07.Text == "")
                    AM_CURRENCY = 0;
                else
                {
                    AM_CURRENCY = Convert.ToDouble(infoDataGridView1.DoSum(9, totalMode.sum));//報價金額
                    RI_EXCHG = Convert.ToDouble(InfoTextBox16.Text);//匯率
                    AM_EXCHG = AM_CURRENCY * RI_EXCHG;//換算金額
                    AM_TAX = AM_EXCHG * RI_TAX;//稅額
                    AM_TOTAL = AM_EXCHG + AM_TAX;//總金額(含稅)
                    //把值丟入TEXTBOX
                    InfoTextBox07.Text = AM_CURRENCY.ToString();//報價金額
                    InfoTextBox1.Text = AM_EXCHG.ToString();//換算金額
                    InfoTextBox31.Text = AM_TAX.ToString();//稅額
                    InfoTextBox21.Text = AM_TOTAL.ToString();//總金額(含稅)
                }
            }
        }
           

        private void InfoRefValBox0_Leave(object sender, EventArgs e)
        {
            //計算金額,弊別改變時
            if ((infoNavigator1.GetCurrentState() == "Inserting") || (infoNavigator1.GetCurrentState() == "Editing"))
            {
                double RI_TAX = 0, AM_EXCHG = 0, AM_CURRENCY = 0, AM_TOTAL = 0, RI_EXCHG = 0, AM_TAX = 0;

                //RI_TAX為稅率
                if (InfoComboBox0.Text == "免稅")
                    RI_TAX = 0;
                else if (InfoComboBox0.Text == "應稅")
                    RI_TAX = 0.05;

                if (InfoTextBox07.Text == "")
                    AM_CURRENCY = 0;
                else
                {

                    AM_CURRENCY = Convert.ToDouble(infoDataGridView1.DoSum(9, totalMode.sum));//報價金額
                    RI_EXCHG = Convert.ToDouble(InfoTextBox16.Text);//匯率
                    AM_EXCHG = AM_CURRENCY * RI_EXCHG;//換算金額
                    AM_TAX = AM_EXCHG * RI_TAX;//稅額
                    AM_TOTAL = AM_EXCHG + AM_TAX;//總金額(含稅)
                    //把值丟入TEXTBOX
                    InfoTextBox1.Text = AM_EXCHG.ToString();//換算金額
                    InfoTextBox31.Text = AM_TAX.ToString();//稅額
                    InfoTextBox21.Text = AM_TOTAL.ToString();//總金額(含稅)
                }
            }
             
        }

        private void ibsMaster_CurrentChanged(object sender, EventArgs e)
        {
        string state = infoNavigator1.GetCurrentState();
        if (state == "Inserting" || state == "Editing" || state == "Initial" || state == "Browsed")
        {
            //修改時間顯示
            string UPDATE_TIME = ((DataRowView)ibsMaster.Current)["UPDATE_TIME"].ToString();
            if (UPDATE_TIME.Length == 6)
            { infoTextBox6.Text = Convert.ToDateTime(UPDATE_TIME.Substring(0, 2) + ":" + UPDATE_TIME.Substring(2, 2) + ":" + UPDATE_TIME.Substring(4, 2)).ToLongTimeString(); }
            else if (UPDATE_TIME.Length == 0)
                InfoTextBox16.Text = "";

            //建檔時間顯示
            string CREATE_TIME = ((DataRowView)ibsMaster.Current)["CREATE_TIME"].ToString();
            if (CREATE_TIME.Length == 6)
            { infoTextBox4.Text = Convert.ToDateTime(CREATE_TIME.Substring(0, 2) + ":" + CREATE_TIME.Substring(2, 2) + ":" + CREATE_TIME.Substring(4, 2)).ToLongTimeString(); }
            else if (CREATE_TIME.Length == 0)
                infoTextBox4.Text = "";
        }        
        }

        public string GetCUST()
        {            
            return ((DataRowView)ibsMaster.Current)["ID_CUST"].ToString();               
        }
       

        private void ibsMaster_AddingNew(object sender, AddingNewEventArgs e)
        {
            //在新增就顯示建檔時間日期            
            infoTextBox4.Text = DateTime.Now.ToLongTimeString();
        }

        private void bindingNavigatorEditItem_Click(object sender, EventArgs e)
        {
            //以string儲存,在按下修改時
            ((DataRowView)ibsMaster.Current)["UPDATE_USER"] = CliUtils.fLoginUser;
            ((DataRowView)ibsMaster.Current)["UPDATE_DATE"] = string.Format("{0:yyyyMMdd}", DateTime.Now.Date);
            ((DataRowView)ibsMaster.Current)["UPDATE_TIME"] = string.Format("{0:hhmmss}", DateTime.Now);

        }

        public string GetBrand()
        {
            //給料品編號選單做參考
            return ((DataRowView)ibsDetail.Current)["NO_BRAND"].ToString();
        }

        private void infoDataGridView1_Click(object sender, EventArgs e)
        {

            //重新設定明細欄位的ReadOnly屬性(因為使用AutoDisableControl會造成回歸預設值False)
            this.cNSEQDataGridViewTextBoxColumn.ReadOnly = true;
            this.nMIMNAMEDataGridViewTextBoxColumn.ReadOnly = true;
            this.cDIMUMDataGridViewTextBoxColumn.ReadOnly = true;
            this.pRVENDDataGridViewTextBoxColumn.ReadOnly = true;
            this.pRSALESDataGridViewTextBoxColumn.ReadOnly = true;
            this.AM_SALES.ReadOnly = true;
            this.NM_VEND.ReadOnly = true;
            this.FG_COUNT.ReadOnly = true;
            this.Column1.ReadOnly = true;
            
        }

        private void infoDataGridView2_Click(object sender, EventArgs e)
        {
            //重新設定明細欄位的ReadOnly屬性(因為使用AutoDisableControl會造成回歸預設值False)
            this.infoDataGridView2.ReadOnly = true;
        }

        private void ibsMaster_AfterEdit(object sender, EventArgs e)
        {
            //以string儲存
            ((DataRowView)ibsMaster.Current)["UPDATE_USER"] = CliUtils.fLoginUser;
            ((DataRowView)ibsMaster.Current)["UPDATE_DATE"] = string.Format("{0:yyyyMMdd}", DateTime.Now.Date);
            ((DataRowView)ibsMaster.Current)["UPDATE_TIME"] = string.Format("{0:hhmmss}", DateTime.Now);

           
        }

        private void ibsDetail_BeforeEdit(object sender, EventArgs e)
        {
            //記錄Detail的修改時間日期
            ((DataRowView)ibsDetail.Current)["UPDATE_USER"] = CliUtils.fLoginUser;
            ((DataRowView)ibsDetail.Current)["UPDATE_DATE"] = string.Format("{0:yyyyMMdd}", DateTime.Now.Date);
            ((DataRowView)ibsDetail.Current)["UPDATE_TIME"] = string.Format("{0:hhmmss}", DateTime.Now);
        }
                
        private void Form1_Load(object sender, EventArgs e)
        {
            //隱藏明細鍵值欄位
            this.Column1.Visible = false;
        }

        private void bindingNavigatorPrintItem_Click(object sender, EventArgs e)
        {
            rGqtQtn1.Form1 Print = new rGqtQtn1.Form1();
            Print.Wstr = "GQT_QTN_M.NO_QTN='" + InfoTextBox01.Text + "'";
            Print.MdiParent = this.MdiParent;
            Print.Show();
        }

        private void infoNavigator1_StateChanged(object sender, InfoNavigatorStateChangedEventArgs e)
        {
            //將DateTimePicker設為不可改
            this.InfoDateTimePicker01.Enabled = false;
            this.InfoDateTimePicker1.Enabled = false;
        }

        private void infoDataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            //計算金額重新  刪除Record時
            if ((infoNavigator1.GetCurrentState() == "Inserting") || (infoNavigator1.GetCurrentState() == "Editing"))
            {
                double RI_TAX = 0, AM_EXCHG = 0, AM_CURRENCY = 0, AM_TOTAL = 0, RI_EXCHG = 0, AM_TAX = 0;

                //RI_TAX為稅率
                if (InfoComboBox0.Text == "免稅")
                    RI_TAX = 0;
                else if (InfoComboBox0.Text == "應稅")
                    RI_TAX = 0.05;

                if (InfoTextBox07.Text == "")
                    AM_CURRENCY = 0;
                else
                {

                    AM_CURRENCY = Convert.ToDouble(infoDataGridView1.DoSum(9, totalMode.sum));//報價金額
                    RI_EXCHG = Convert.ToDouble(InfoTextBox16.Text);//匯率
                    AM_EXCHG = AM_CURRENCY * RI_EXCHG;//換算金額
                    AM_TAX = AM_EXCHG * RI_TAX;//稅額
                    AM_TOTAL = AM_EXCHG + AM_TAX;//總金額(含稅)
                    //把值丟入TEXTBOX
                    InfoTextBox07.Text = AM_CURRENCY.ToString();//報價金額
                    InfoTextBox1.Text = AM_EXCHG.ToString();//換算金額
                    InfoTextBox31.Text = AM_TAX.ToString();//稅額
                    InfoTextBox21.Text = AM_TOTAL.ToString();//總金額(含稅)
                }
            }
        }

        private void bindingNavigatorAddNewItem_MouseUp(object sender, MouseEventArgs e)
        {
            //將DateTimePicker設為不可改
            this.InfoDateTimePicker01.Enabled = false;
            this.InfoDateTimePicker1.Enabled = false;
        }

        public bool CheckDetail(object Value)
        {
            //檢查Detail是否為空
            if (ibsDetail.List.Count == 0)
                return false;
            else
                return true;
        }
    }
}

